﻿using System;
using System.Web.UI;
using Demo.Extensions;
using Carbon.Web.Forms.Extensions;
using Carbon.Web.Forms;
using System.Web.UI.WebControls;
using System.Web;

namespace Demo.Demos.Poll
{
    public partial class Poll : DemoPage
    {
		#region Fields (1)
		protected EditPollDataContext ctrl_DataContext = null;
		#endregion

		#region Properties (2)
		public EditPollDataContext Control_DataContext
        {
            get
            {
                if (ctrl_DataContext == null) throw new NullReferenceException();
                return ctrl_DataContext;
            }
        }
		public PollViewModel Model
        {
            get
            {
                return Control_DataContext.ViewModel;
            }
        }
		#endregion

		#region Methods (1)
        
        protected DropDownList ctrl_DropDownList = null;
        public DropDownList Control_DropDownList
        {
          get
          {
             if (ctrl_DropDownList == null) throw new NullReferenceException();
            return ctrl_DropDownList;
          }
        }

        protected override ViewStatePersistanceMode ViewStatePersistanceMode
        {
            get
            {
                string viewStateMode = this.Page.Request["ViewStateMode"];
                if (string.IsNullOrEmpty(viewStateMode))
                {
                    viewStateMode = "Client";
                }
    
                switch (viewStateMode)
                {
                    case "Client":
                        return Carbon.Web.Forms.ViewStatePersistanceMode.Client;
                    case "Server":
                        return Carbon.Web.Forms.ViewStatePersistanceMode.Database;
                    case "ServerCompressed":
                        return Carbon.Web.Forms.ViewStatePersistanceMode.CompressedDatabase;
                }

                return Carbon.Web.Forms.ViewStatePersistanceMode.Client;
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            if (!IsPostBack)
            {
                string viewStateMode = this.Page.Request["ViewStateMode"];
                if (string.IsNullOrEmpty(viewStateMode)) {
                    viewStateMode = "Client";
                }

                Control_DropDownList.SelectedValue = viewStateMode;
            }
        }

        protected void Changed_ViewStateMode(object sender, EventArgs args)
        {
            HttpContext context = HttpContext.Current;
            String url = DemoUtility.GetUrlWithParameter(context.Request.Url.GetComponents(UriComponents.PathAndQuery, UriFormat.Unescaped), "ViewStateMode", Control_DropDownList.SelectedValue);

            this.Page.Response.Redirect(url);
        }
        
		public String GetImageUrl(Control context)
        {
            ViewModelEntityFramework model = (ViewModelEntityFramework)context.GetViewModelContext();
            return ImageHandler.GetFileReferenceImageUrl(model.Avatar);
        }

		#endregion

		#region Event Handlers (4)

		protected void AddAnswer_Click(object sender, EventArgs args)
        {
            Control_DataContext.ViewModel.AddNewAnswer();
        }

		protected void RemoveAnswer_Click(object sender, EventArgs args)
        {
            AnswerViewModel viewModel = (AnswerViewModel)((Control)sender).GetViewModelContext();

            Control_DataContext.ViewModel.RemoveAnswer(viewModel);
        }

        protected void Save_Click(object sender, EventArgs args)
        {
            this.SafeExecute(() =>
            {
                if (!this.ValidateRecursively())
                {
                    throw new ApplicationException("Data not valid");
                }

                throw new ApplicationException("Success");
            });
        }

        
        protected void Popup_ReturnedData(object sender, PopupViewModelChangedEventArgs<Object> args)
        {
            AnswerViewModel awm = (AnswerViewModel)args.ViewModel;
            this.Control_DataContext.ForceSetViewModel(awm.Parent);
        }

        protected void Popup_GetData(object sender, PopupGetViewModelEventArgs<Object> args)
        {
            AnswerViewModel answerViewModel = (AnswerViewModel)((Control)sender).GetViewModelContext();
            args.ViewModel = answerViewModel;
        }

        public int ModelSize
        {
            get
            {
                return InternalUtility.CompactSerializeToByteArray(Control_DataContext.ViewModel).Length;
            }
        }
        
        protected void Validate_Answer(object sender, ValidatorEventArgs args)
        {
            AnswerViewModel answerViewModel = (AnswerViewModel)((Control)sender).GetViewModelContext();
            answerViewModel.Validate();
        }
        
		protected void Validate_Answers(object sender, EventArgs args)
        {
            Model.ValidateAnswers();
        }

		#endregion
    }
}